<?php
/*
 * @Descripttion: 
 * @version: v1
 * @Author: Terry
 * @Date: 2021-10-11 12:20:11
 * @LastEditors: Terry
 * @LastEditTime: 2024-06-21 12:53:08
 */
declare (strict_types = 1);

namespace app\openApi\middleware;
use think\exception\ValidateException;

class CheckData
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next){
        $data = $request->post();
        if(isset($data['data']))$data = $data['data'];
        $route =   strtolower($request->pathinfo());
        $route  = explode('/',$route);
        $rule = config('rule.'.$route[2].'.'.$route[3]) ?? [];
        try {
            validate($rule)->check($data);
            $request->data = $data;
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return result(FAIL,'',$e->getError());
        }
        return $next($request);
    }
}
